#pragma once

#include <QObject>
#include <qcustomplot.h>

class PlotController : public QObject
{
    Q_OBJECT
public:
    explicit PlotController(QCustomPlot *plot, QObject *parent = nullptr);
    ~PlotController();

    bool eventFilter(QObject *watched, QEvent *event) override;

    void setBackground(const QBrush &background);
    void setAxisRectBackground(const QBrush &background);
    void setAxisRectRangeZoomFactor(double x = 1.0, double y = 1.0);
    void setInteraction(const QCP::Interaction &interaction, bool enabled = true);
    void setInteractions(const QCP::Interactions &interactions = QCP::iRangeDrag |
                                                                 QCP::iRangeZoom |
                                                                 QCP::iSelectAxes |
                                                                 QCP::iSelectLegend |
                                                                 QCP::iSelectPlottables);
    void replot(QCustomPlot::RefreshPriority refreshPriority = QCustomPlot::rpRefreshHint);

    int addGraph(const QString &name = QString());
    QCPGraph *graph(int index);
    void addData(int graphIndex, double key, double value);
    void setGraphName(int index, const QString &name);
    void setGraphPen(int index, const QPen &pen);
    void setGraphBrush(int index, const QBrush &brush);
    void setGraphAntialiasedAll(int index, bool on = true);
    void setGraphChannelFillGraph(int mainIndex, int otherIndex);
    void setGraphLineStyle(int index, QCPGraph::LineStyle style);
    void setGraphScatterStyle(int index, QCPScatterStyle style);
    void rescaleGraphAxes(int index, bool onlyEnlarge = false);
    void rescaleGraphKeyAxis(int index, bool onlyEnlarge = false);
    void rescaleGraphValueAxis(int index, bool onlyEnlarge = false);
    void setTracerEnabled(bool enabled = true);

    //x-0 y-1 x2-2 y2-3
    QCPAxis *axis(int index);
    void setAxisVisible(int index, bool visible);
    void setupFullAxesBox(bool connectRanges = false);
    void setAxisConnectRanges(int index, bool connectRanges = true);
    void setGridPen(int index, const QPen &pen);
    void setSubGridPen(int index, const QPen &pen);
    void setSubGridVisible(int index, bool visible);
    void setAxisLabel(int index, const QString &label);
    void setAxisLabelColor(int index, const QColor &color);
    void setAxisTickLabelVisible(int index, bool visible);
    void setAxisTickLabelColor(int index, const QColor &color);
    void setAxisTickLabelSide(int index, QCPAxis::LabelSide side = QCPAxis::lsInside);
    void setAxisTickLabelRotation(int index, double rotation);
    void setAxisRange(int index, double min, double max);
    void setAxisPen(int index, const QPen &pen);
    void setAxisTickPen(int index, const QPen &pen);
    void setAxisSubTickPen(int index, const QPen &pen);
    void setAxisLowerEnding(int index, const QCPLineEnding &ending);
    void setAxisUpperEnding(int index, const QCPLineEnding &ending);


    void setLegendVisible(bool visible);

    void setSelectionRectPen(const QPen &pen);
    void setSelectionRectBrush(const QBrush &brush);
    void setSelectionRectMode(const QCP::SelectionRectMode &mode);

signals:

protected:
    QCustomPlot *m_plot;
    bool m_tracerEnabled = false;
};

